<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/post_entity', 'Post_Entity');
    }

    public function countPost($option=array())
    {
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        if(count($option)==0){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
        } else {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function pagingPost($limit, $offset, $options=array())
    {
        $select = "a.id, c.image_folder, c.full_name as user_full_name, c.role, a.description, a.title, a.image, a.slug, content, a.order, a.publish_flg, a.created_at, a.deleted_at,
        b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        if(count($options)==0){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
        } else {
            $this->db->where($options);
        }
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.id', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('a.created_at', 'DESC');
        return $this->db->get()->result('Post_Entity');
    }

    public function countSearchPost($search_key, $options=array(), $is_admin = false)
    {
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->like('a.title', $search_key);
        $this->db->or_like('a.description', $search_key);
        $this->db->or_like('a.slug', $search_key);
        $this->db->or_like('b.title', $search_key);
        $this->db->or_like('b.slug', $search_key);
        if(!$is_admin){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
        }
        if(count($options) > 0){
            $this->db->where($options);
        }
        return $this->db->count_all_results();
    }

    public function searchPagingPost($search_key, $limit, $offset, $options=array(), $is_admin=false)
    {
        $select = "a.id, c.image_folder, c.full_name as user_full_name, c.role, a.description, a.title, a.image, a.slug, content, a.order, a.publish_flg, a.created_at, a.deleted_at,
        b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->like('a.title', $search_key);
        $this->db->or_like('a.description', $search_key);
        $this->db->or_like('a.slug', $search_key);
        $this->db->or_like('b.title', $search_key);
        $this->db->or_like('b.slug', $search_key);
        if(!$is_admin){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
        }
        if(count($options) > 0){
            $this->db->where($options);
        }
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.id', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('a.created_at', 'DESC');
        return $this->db->get()->result('Post_Entity');
    }

    public function getPost($slug)
    {
        $select = "a.id, a.description, a.title, a.image, a.slug, a.content, a.order, a.publish_flg, a.created_at,
        b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug, c.image_folder";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('a.slug', $slug);
        return $this->db->get()->first_row('Post_Entity');
    }

    public function getLatestPosts()
    {
        $select = "a.id AS post_id, a.description AS post_description, a.title AS post_title,a.image AS post_image,a.slug AS post_slug, a.content AS post_content, b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->where('a.publish_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->limit(5);

        return $this->db->get()->result();
    }

    public function getNextPost($id)
    {
        $select = "a.id, a.description, a.title, a.image, a.slug, content, a.created_at, b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->where('a.publish_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('a.id >', $id);
        $this->db->order_by('a.id', ' ASC');
        $this->db->limit(1);
        $result = $this->db->get()->first_row('Post_Entity');
        if (count($result) > 0) {
            return $result;
        }
        return null;
    }

    public function getPrevPost($id)
    {
        $select = "a.id, a.description, a.title, a.image, a.slug, content, a.created_at, b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->where('a.publish_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('a.id <', $id);
        $this->db->order_by('a.id', ' DESC');
        $this->db->limit(1);
        $result = $this->db->get()->first_row('Post_Entity');
        if (count($result) > 0) {
            return $result;
        }
        return null;
    }

    /**
     * For admin only
     * Add new post
     */
    public function insert($post)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_post_name, $post);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a post
     */
    public function update($post_id, $post)
    {
        $this->db->trans_begin();
        $this->db->where('id', $post_id);
        $this->db->update($this->table_post_name, $post);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function checkPostExists($slug, $id=null){
        $select = "a.id";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->where('a.slug', $slug);
        if($id!=null){
            $this->db->where('a.id <>', $id);
        }
        return $this->db->count_all_results();
    }

    public function countPostByCategory($category, $option=array()){
        $this->db->select('a.id');
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        if(count($option)==0){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('b.slug', $category);
        } else {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function getByCategory($limit, $offset, $category, $aCol = array(), $options=array()){
        $select = "a.id, a.description, a.title, a.image, a.slug, content, a.order, a.publish_flg, a.created_at, a.deleted_at,
        b.id AS post_cate_id, b.title AS post_cate_title, b.slug AS post_cate_slug, c.image_folder";
        $this->db->select($select, true);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'a.post_cate_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        if(count($options)==0){
            $this->db->where('a.publish_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('b.slug', $category);
        } else {
            $this->db->where($options);
        }
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.id', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('a.created_at', 'DESC');
        return $this->db->get()->result('Post_Entity');
    }
}
?>